<?php
class Wxbind extends BZ_Controller
{
    public function _init()
    {
       header("Content-type: text/html; Charset=utf-8");//载入微信页面设置字符集
       $this->load->model('user_model','user');
       $this->load->model('bind_user_model','bind_user');
    }
    
    /**
     *获取微信用户基本信息
    */
    public function grid()
    {
        if(!isset($_GET['code']))
        {
            $return_url = urlencode(site_url('home/wxbind/grid'));
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3c5437a8f56b384e&redirect_uri=".$return_url."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
            header("Location:$url");
        }else{
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3c5437a8f56b384e&secret=d54eef1638a1c094d33e15bf2d0b1f66&code=".$_GET['code']."&grant_type=authorization_code";
            $resource = $this->getUrlInfo($url);
            $accessToken = $resource->access_token;
            $openid = $resource->openid;
            $urlOne = "https://api.weixin.qq.com/sns/userinfo?access_token=".$accessToken."&openid=".$openid."&lang=zh_CN";
            $userInfo = $this->getUrlInfo($urlOne);
            $this->lottery($userInfo,$openid);
        }
    }
    
     /**
     * 载入奖品视图
     * @param unknown $userInfo
     */
    public function lottery($userInfo,$openid)
    {
    	$data['openid']   = $userInfo->openid;
    	$data['nickname'] = $userInfo->nickname;
    	echo $data['openid'].'<br/>'.$data['nickname'];
    	var_dump($userInfo);
    	exit;
    }
    
    
    
    
    
     /**
     * @param unknown $url
     */
    public function getUrlInfo($url)
    {
        $ch  = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);   //返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //可以设置为验证站点
        $result = curl_exec($ch);
        curl_close($ch);//解析 得到openid,打开绑定页面 传openid
        $resource = json_decode($result);
        return $resource;
    }
}
